<template>
  <div>
    <!-- 1.0搜索框 -->
    <van-search
      shape="round"
      class="search"
      v-model.lazy="input"
      show-action
      placeholder="搜索目的地"
      @search="onSearch"
    >
      <template #action>
        <span class="cityName">{{ this.$route.query.city || "重庆" }}</span>
        <img
          style="width: 20px"
          src="http://qzek6s6bf.hn-bkt.clouddn.com/dizhi.png"
          alt=""
          @click="onSearch1"
        />
      </template>
    </van-search>

    <!-- 2.0滑动插件 -->
    <HomeSwiper></HomeSwiper>

    <div class="clear"></div>

    <!-- 3.0 当前热门信息 -->
    <!--     <div class="hot">
      <h1>
        当前热门<img
          style="width: 20px; margin-left: 2%"
          src="http://qzek6s6bf.hn-bkt.clouddn.com/huore.png"
          alt=""
        />
      </h1>
      <van-grid :border="false" :column-num="3" :gutter="0">
        <van-grid-item v-for="(value, index) of imgList" :key="index">
          <van-image :src="value.src" :square="true" />
        </van-grid-item>
      </van-grid>
    </div> -->

    <!-- 3.0  图标列表 -->
    <div>
      <van-grid icon-size="40px" :border="false" clickable :column-num="4">
        <van-grid-item
          v-for="(value, index) of titleList"
          :key="index"
          :icon="value.icon"
          :text="value.text"
          :to="value.to"
        />
      </van-grid>
    </div>
    <div class="line"></div>
    <!-- 4.0猜你喜欢信息 -->
    <div class="container">
      <h1>
        猜你喜欢
        <img
          style="width: 20px; margin-left: 2%"
          src="http://qzek6s6bf.hn-bkt.clouddn.com/aixin.png"
          alt=""
        />
      </h1>
      <ul v-for="(value, index) of addressList" :key="index">
        <li @click="toaddressDetails(value, index)">
          <div class="showImg">
            <img :src="value.imgsrc" alt="" />
          </div>
          <div class="showText">
            <h1 class="title">{{ value.title }}</h1>
            <p class="evaluate">{{ value.evaluate }}</p>
            <p class="address">{{ value.address }}</p>
            <img
              @click.stop="changecollectImgSrc(value)"
              class="collect"
              style="width: 0.5rem"
              :src="value.collectImgSrc"
              alt=""
            />
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import axios from "axios";
// @ is an alias to /src
/* import HelloWorld from '@/components/HelloWorld.vue' */
import HomeSwiper from "./HomeSwiper.vue";

export default {
  props: {},
  name: "Home",
  components: {
    /*   HelloWorld */
    HomeSwiper,
  },
  data() {
    return {
      nowTime: "",
      value1: "123",
      active: 0,
      input: "",
      show: false,
      username: "",
      /* titleList */
      titleList: [],
      /* 3.0当前热门数组 */
      imgList: [],
      /* 4.0猜你喜欢数组 */
      addressList: [],
      addressList1: [],
      myCollects: JSON.parse(localStorage.getItem("myCollects")) || [],
    };
  },
  methods: {
    /*  收藏 */
    changecollectImgSrc(value) {
      /* console.log(value.collectImgSrc); */
      if (
        value.collectImgSrc == "http://qzek6s6bf.hn-bkt.clouddn.com/aixin0.png"
      ) {
        value.collectImgSrc = "http://qzek6s6bf.hn-bkt.clouddn.com/aixin1.png";
        this.nowDate();
        /* console.log(value.title); */
        this.myCollects.unshift({
          title: value.title,
          imgsrc: value.imgsrc,
          nowTime: this.nowTime,
        });
        /*   console.log(this.myCollects); */
        localStorage.setItem("myCollects", JSON.stringify(this.myCollects));
      } else {
        value.collectImgSrc = "http://qzek6s6bf.hn-bkt.clouddn.com/aixin0.png";
        console.log(value.title);
        this.myCollects.map((item, index) => {
          if (value.title == item.title) {
            this.myCollects.splice(index, 1);
          }
        });

        /*   console.log(this.myCollects); */
        localStorage.setItem("myCollects", JSON.stringify(this.myCollects));
      }
    },
    onSearch() {
      this.addressList = this.addressList1.filter((value) => {
        return value.title.match(this.input) || value.address.match(this.input);
      });
    },
    onSearch1() {
      this.$router.push("/cityList");
    },
    showPopup() {
      this.show = true;
    },
    toMore() {
      this.$router.replace("/scenic");
    },
    toaddressDetails(value) {
      this.$router.push({
        path: "/addressDetails",
        query: {
          title: value.title,
          produce: value.produce,
          imgList: value.imgList,
        },
      });
    },
    /*  */
    laodTitleList() {
      axios.get("/api/titleList").then((res) => {
        /*      console.log(res.data.data.titleList); */
        this.titleList = res.data.data.titleList;
      });
    },
    /* 4.0猜你喜欢列表请求数据 */
    loadAddressList() {
      axios.get("/api/addressList").then((res) => {
        const result = res.data.data.addressList;
        this.myCollects.map((item) => {
          /*           console.log(item.title); */
          result.map((value) => {
            /*    console.log(value); */
            if (item.title == value.title) {
              value.collectImgSrc =
                "http://qzek6s6bf.hn-bkt.clouddn.com/aixin1.png";
            }
          });
        });
        /*        console.log(res.data.data.addressList); */
        this.addressList = result;
        this.addressList1 = result;
      });
    },
    /* 3.0当前热门信息 */
    loadImgList() {
      axios.get("/api/imgList").then((res) => {
        /* console.log(res.data.data.imgList); */
        this.imgList = res.data.data.imgList;
      });
    },
    /* 获取当前时间方法 */
    nowDate() {
      const nowDate = new Date();
      const date = {
        year: nowDate.getFullYear(),
        month: nowDate.getMonth() + 1,
        date: nowDate.getDate(),
        hours: nowDate.getHours(),
        Minutes: nowDate.getMinutes(),
      };
      const newmonth = date.month > 10 ? date.month : "0" + date.month;
      const day = date.date > 10 ? date.date : "0" + date.date;
      const Minute =
        nowDate.getMinutes() < 10
          ? "0" + nowDate.getMinutes()
          : nowDate.getMinutes();
      this.nowTime =
        date.year +
        "-" +
        newmonth +
        "-" +
        day +
        " " +
        date.hours +
        ":" +
        Minute;
    },
  },
  created() {
    var username = localStorage.getItem("username");
    this.username = username;
    this.laodTitleList();
    this.loadAddressList();
    this.loadImgList();
  },
};
</script>

<style scoped>
.cityName {
  margin-left: 0.2rem;
  margin-right: 0.2rem;
}
.search {
  width: 100%;
  margin: 0 auto;
  /*   position: absolute; */
  z-index: 999;
  opacity: 0.9;
  background-color: rgb(115, 236, 245);
}

.clear {
  clear: both;
}
.container {
  margin: 0 auto;
  width: 96%;
  height: 15rem;
  margin-top: 3%;
  background-color: rgb(247, 247, 247);
}
.container h1 {
  padding-top: 3%;
}
h1 {
  font-size: 16px;
  margin-left: 4%;
  margin-top: 5%;
}
.more {
  margin-left: 60%;
}
.line {
  width: 100%;
  border: 5px solid rgb(233, 230, 230);
  background-color: rgb(233, 230, 230);
  margin-bottom: 4%;
}
.hot {
  width: 96%;
  margin-left: 2%;
}
p {
  margin-top: 3%;
}
.showImg {
  position: absolute;
  width: 50%;
  height: 100%;
}
.showImg img {
  width: 96%;
  margin-left: 2%;
  margin-top: 5%;
}
.showText {
  position: absolute;
  left: 50%;
  width: 50%;
  height: 100%;
}
.title {
  margin-top: 15%;
  font-size: 18px;
}
.evaluate {
  margin-top: 10%;
  margin-left: 5%;
  font-size: 12px;
}
.address {
  margin-top: 10%;
  margin-left: 5%;
  font-size: 14px;
}
ul {
  position: relative;
  margin-top: 3%;
  width: 100%;
  height: 2.7rem;
  border-radius: 3%;
  background-color: rgb(255, 255, 255);
}
.collect {
  position: absolute;
  left: 90%;
  top: 22%;
  z-index: 99999;
}
</style>